
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class SymbolPanel extends JPanel{
	
	private BufferedImage image;
	private String condition = "";
	
	public SymbolPanel(String s){
		
		try {                
	          image = ImageIO.read(new File(s));
	       } catch (IOException ex) {
	            throw new RuntimeException(ex);
	       }
		
		this.addMouseMotionListener(new DragListiner());
		this.addMouseListener(new DialogListiner());
	}
	
	public void dragging(MouseEvent event){
		Point pt = SwingUtilities.convertPoint(this, event.getPoint(), this.getParent());
		setBounds(event.getX(), event.getY(), getWidth() , getHeight());
	}
	
	@Override
	public void paintComponent(Graphics g){
		
		 super.paintComponent(g);
		 
	     g.drawImage(image, 0, 0, null);
	     g.drawString(condition, 0 ,0);
	}
	
	private class DialogListiner implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			
			if(e.getClickCount() == 2){
				SymbolPanel.this.condition = JOptionPane.showInputDialog("Enter the Condition", condition);
				SymbolPanel.this.repaint();
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	private class DragListiner implements MouseMotionListener{

		@Override
		public void mouseDragged(MouseEvent arg0) {
			SymbolPanel.this.dragging(arg0);
		}

		@Override
		public void mouseMoved(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}
}